<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="12" :offset="7">
          <el-date-picker
            v-model="dateValue"
            align="center"
            type="date"
            size="small"
            placeholder="选择日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <div id="myChart"  class="chart-box"></div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        },
        dateValue: new Date(),
        dayPayData: {
          type: ['其他类型', '食物类', '住房类', '生活类', '日化类', '通讯类', '交通类', '工资', '转账'],
          data: [
            {value: 335, name: '其他类型'},
            {value: 310, name: '食物类'},
            {value: 234, name: '住房类'},
            {value: 135, name: '生活类'},
            {value: 43, name: '日化类'},
            {value: 546, name: '通讯类'},
            {value: 23, name: '交通类'},
            {value: 3255, name: '工资'},
            {value: 1548, name: '转账'}
          ]
        }
      }
    },
    mounted () {
      this.drawLine()
    },
    methods: {
      drawLine () {
        // 基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('myChart'))
        // 绘制图表
        myChart.setOption({
          title: {
            text: '每日收入明细',
            left: 'center'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            orient: 'vertical',
            left: 'left',
            data: this.dayPayData.type
          },
          series: [
            {
              name: '收支类型',
              type: 'pie',
              radius: '55%',
              center: ['50%', '60%'],
              data: this.dayPayData.data,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              },
              itemStyle: {
                normal: {
                  label: {
                    show: true,
                    formatter: '{b} : {c}'
                  },
                  labelLine: { show: true }
                }
              }
            }
          ]
        })
      },
      getSectorList () {
        this.$http({
          url: this.$http.adornUrl(''),
          method: 'get',
          params: this.$http.adornParams({
            'dateValue': this.dateValue
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dayPayData = data
          } else {
            this.dayPayData = {}
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .chart-box {
    min-height: 500px;
  }
</style>
